package com.xxx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xxx.common.Code;
import com.xxx.common.Result;
import com.xxx.dto.PackageDto;
import com.xxx.pojo.ClassInfo;
import com.xxx.pojo.Dish;
import com.xxx.pojo.Package;
import com.xxx.service.ClassInfoService;
import com.xxx.service.PackageService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/package")
public class PackageController {

    @Autowired
    private PackageService packageService;

    @Autowired
    private ClassInfoService classInfoService;

    /**
     * 新增
     * @param ipackage
     * @return
     */
    @RequiresRoles(value = {"admin","manage","employee"},logical = Logical.OR)
    @PostMapping
    public Result save(@RequestBody Package ipackage){
        log.info("ipackage:{}",ipackage);
        boolean save = packageService.save(ipackage);


        return save ? new Result(null,Code.SAVE_OK,"添加成功~~~") : new Result(null,Code.SAVE_ERR,"500 系统错误！！！");
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequiresRoles(value = {"admin","manage"},logical = Logical.OR)
    @DeleteMapping("/{ids}")
    public Result deleteByIds(@PathVariable List<Long> ids){

        boolean b = packageService.removeBatchByIds(ids);
        Result result = new Result();
        if (b){
            result.setCode(Code.DELETE_OK);
            result.setMessage("删除成功~~~");
        }else {
            result.setCode(Code.DELETE_ERR);
            result.setMessage("500 系统错误，删除失败！！！");
        }
        return result;

    }


    /**
     * 修改状态
     * @param status
     * @param ids
     * @return
     */
    @RequiresRoles(value = {"admin","manage","employee"},logical = Logical.OR)
    @PutMapping("/{status}/{ids}")
    public Result updateStatus(@PathVariable Integer status,@PathVariable List<Long> ids){
        int i = packageService.updateStatus(status, ids);
        return i > 0 ?
                new Result(null,Code.UPDATE_OK,"修改成功~~~") :
                new Result(null,Code.UPDATE_ERR,"500 系统错误，膝修改失败！！！");
    }

    /**
     * 修改菜品信息
     * @param ipackage
     * @return
     */
    @PutMapping("/update")
    @RequiresRoles(value = {"admin","manage","employee"},logical = Logical.OR)
    public Result update(@RequestBody Package ipackage){
        boolean update = packageService.updateById(ipackage);
        return update ?
                new Result(null,Code.UPDATE_OK,"修改成功~~~") :
                new Result(null,Code.UPDATE_ERR,"500 系统错误，膝修改失败！！！");

    }

    /**
     * 分页查询
     * @param pageNo 页数
     * @param pageSize 单页容量
     * @return
     */
    @RequiresRoles(value = {"admin","manage","employee"},logical = Logical.OR)
    @GetMapping
    public Result getPackageByPage(int pageNo,int pageSize,String name){
        Page page = new Page<>(pageNo,pageSize);

        LambdaQueryWrapper<Package> lqw = new LambdaQueryWrapper<>();

        lqw.like(name != null && !name.trim().isEmpty(),Package::getName,name);
        page = packageService.page(page, lqw);

        List<Package> records = page.getRecords();

        List<PackageDto> dtoList = records.stream().map(aPackage -> {
            PackageDto packageDto = new PackageDto();

            BeanUtils.copyProperties(aPackage,packageDto);

            ClassInfo classInfo = classInfoService.getTypesNameById(aPackage.getTypesId());
            packageDto.setTypesName(classInfo.getTypesName());
            return packageDto;
        }).collect(Collectors.toList());

        log.info("套餐详情：{}",dtoList);

        page.setRecords(dtoList);
        Result result = new Result(page, Code.GET_OK);
        return result;
    }

    @RequiresGuest
    @GetMapping("/list")
    public Result getPackage(Long id){
        List<Package> packageList = packageService.list(new LambdaQueryWrapper<Package>().eq(Package::getTypesId, id).eq(Package::getStatus,1));

        return packageList != null ? new Result(packageList,Code.GET_OK) : new Result(null,Code.GET_ERR,"无数据");
    }

}
